# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline
from scrapy import Request

class TSpiderPipeline(object):
    def process_item(self, item, spider):
        if item['title'][0].strip("") == "":
            raise DropItem("Is Empty,Drop it")
        else:
            return item

class ImageDownloadPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield Request(image_url)

    def item_completed(self,result,item,info):
        print("\033[1;32m[+]\033[0m Download Success")
        image_paths = [x['path'] for ok,x in result if ok]
        if not image_paths:
            raise DropItem("没有图片需要下载")
        item['image_paths'] = image_paths
        return item
